<template>
  <div class="register">
    <div class="register-main">
        <div class="register-main-title">
          <span>黑马大事件后台管理系统</span>
        </div>
        <div class="register-main-content">
          <el-form :model="registerInfo" :rules="rules" status-icon ref="registerForm" size="medium">
            <el-form-item prop="userAccount">
              <el-input v-model="registerInfo.userAccount" placeholder="请输入账号"></el-input>
            </el-form-item>
            <el-form-item prop="userPassword">
              <el-input v-model="registerInfo.userPassword" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item prop="userRePassword">
              <el-input v-model="registerInfo.userRePassword" placeholder="请再次输入密码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="register" style="width:350px;margin-top:20px">注册</el-button>
            </el-form-item>
            <div class="login" style="float:right;">
              <span>已有账号?<router-link to="/login" style="color:#fff;">去登录</router-link></span>
            </div>
          </el-form>
        </div>
    </div>
  </div>
</template>

<script>
import { reqRegister } from '@/api/user.js';
export default {
    name:"register",
    data(){
      const validateCount = (rule,value,callback)=>{
        if(value.trim() == ""){
          return callback(new Error("账号不能为空!"));
        }else{
          callback();
        }
      }
      const validatePass = (rule,value,callback)=>{
        if(value.trim() == ""){
          return callback(new Error("密码不能为空!"))
        }else{
          callback();
        }
      }
      const validatePass2 = (rule, value, callback) => {
        if (value.trim() === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerInfo.userPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        registerInfo:{
          userAccount:"",
          userPassword:"",
          userRePassword:""
        },
        //表单校验规则
        rules: {
          userAccount: [
            { validator: validateCount, trigger: 'blur' }
          ],
          userPassword: [
            { validator: validatePass, trigger: 'blur' }
          ],
          userRePassword: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      register(){
        this.$refs.registerForm.validate(async(valid) => {
          if (valid) {
            //进行注册
            let res = await reqRegister({username:this.registerInfo.userAccount,password:this.registerInfo.userPassword,repassword:this.registerInfo.userRePassword});
            if(res.code !== 0) return this.$message({type:"warning",message:"注册失败,请重试!"});
            this.$router.push({name:"login",path:"/login"});
            this.$message({type:"info",message:"注册成功!"});
          } else {
            //验证未通过，阻止注册
            this.$message({type:"warning",message:"请输入注册信息!"});
          }
        })
      }
    }
}
</script>

<style scoped>
.register{
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    background: url(@/assets/image/img_01.jpg) no-repeat;
    background-size: cover;
}
.register .register-main{
    width:500px;
    height:400px;
    background: rgba(114, 114, 114, 0.8);
}
.register .register-main .register-main-title{
  width:100%;
  height: 80px;
  text-align: center;
  line-height: 80px;
  font-size:25px;
  font-weight: bold;
}
.register .register-main .register-main-content{
  width: 70%;
  height: 250px;
  margin: 0 auto;
}
</style>